<template>
  <div class="app-container">
    <el-form :model="form" label-width="85px">
      <el-row :gutter="20">
        <el-col :span="6">
          <el-form-item label="设备编号" prop="code">
            {{ form.code }}
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="设备名称" prop="name">
            {{ form.name }}
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="工厂" prop="factoryName">
            {{ form.factoryName }}
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="工段" prop="workshopName">
            {{ form.workshopName }}
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="产线" prop="productionLineName">
            {{ form.productionLineName }}
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="工艺段" prop="processSectionName">
            {{ form.processSectionName }}
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="设备类型" prop="typeName">
            {{ form.typeName }}
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="状态" prop="status">
            <dict-tag :options="equipment_status" :value="form.status" />
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="厂家" prop="manufacturer">
            {{ form.manufacturer }}
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="设备型号" prop="model">
            {{ form.model }}
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="备注" prop="remark">
            {{ form.remark }}
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>

    <el-tabs
      v-model="activeName"
      v-hasPermi="['base:point:list']"
      class="demo-tabs"
    >
      <el-tab-pane label="设备点位" name="first">
        <el-form
          :model="queryParams"
          ref="queryRef"
          :inline="true"
          v-show="showSearch"
          label-width="100px"
          class="form-search"
        >
          <el-form-item label="点位标识" prop="identification">
            <el-input
              v-model="queryParams.identification"
              placeholder="请输入点位标识"
              clearable
              style="width: 240px"
              @keyup.enter="handleQuery"
            />
          </el-form-item>
          <el-form-item label="点位名称" prop="description">
            <el-input
              v-model="queryParams.description"
              placeholder="请输入点位名称"
              clearable
              style="width: 240px"
              @keyup.enter="handleQuery"
            />
          </el-form-item>

          <el-form-item>
            <el-button type="primary" icon="Search" @click="handleQuery"
              >搜索</el-button
            >
            <el-button icon="Refresh" @click="resetQuery">重置</el-button>
          </el-form-item>
        </el-form>
        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button
              v-hasPermi="['base:point:add']"
              type="primary"
              plain
              icon="Plus"
              @click="handleAdd"
              >新增</el-button
            >
          </el-col>
          <el-col :span="1.5">
            <el-button
              type="success"
              v-hasPermi="['base:point:edit']"
              plain
              icon="Edit"
              :disabled="single"
              @click="handleUpdate"
              >编辑</el-button
            >
          </el-col>
          <!-- <el-col :span="1.5">
            <el-button
              type="danger"
              plain
              v-hasPermi="['base:point:delete']"
              icon="Delete"
              :disabled="multiple"
              @click="handleDelete"
              >删除</el-button
            >
          </el-col> -->
          <right-toolbar
            v-model:showSearch="showSearch"
            @queryTable="getList"
          ></right-toolbar>
        </el-row>
        <el-table
          :data="pointData"
          border
          @selection-change="handleSelectionChange"
          ref="whStorehouseInDetail"
        >
          <el-table-column type="selection" width="50" align="center" />
          <el-table-column label="点位标识" prop="identification">
          </el-table-column>
          <el-table-column label="点位名称" prop="description">
          </el-table-column>

          <el-table-column label="数据类型" prop="dataType"> </el-table-column>
          <el-table-column label="采集数据" prop="collectData">
          </el-table-column>
          <el-table-column label="采集时间" prop="collectTime">
            <template #default="scope">
              <span>{{ parseTime(scope.row.collectTime) }}</span>
            </template>
          </el-table-column>
          <el-table-column
            label="操作"
            align="center"
            width="160"
            class-name="small-padding fixed-width"
          >
            <template #default="scope">
              <el-button
                link
                v-hasPermi="['base:point:edit']"
                type="primary"
                icon="Edit"
                @click="handleUpdate(scope.row)"
                >编辑</el-button
              >
              <el-button
                link
                v-hasPermi="['base:point:delete']"
                type="primary"
                icon="Delete"
                @click="handleDelete(scope.row)"
                >删除</el-button
              >
            </template>
          </el-table-column>
        </el-table>
      </el-tab-pane>
    </el-tabs>
    <!-- 添加或修改参数配置对话框 -->
    <el-dialog
      :title="title"
      v-model="open"
      :close-on-click-modal="false"
      width="700px"
      append-to-body
    >
      <el-form
        ref="formRef"
        :model="binForm"
        :rules="rules"
        label-width="100px"
      >
        <el-form-item label="点位标识" prop="identification">
          <code-input
            v-model="binForm.identification"
            code="EQUIPMENT_CODE"
            :disabled="!!binForm.id"
            placeholder="请输入点位编号"
          />
        </el-form-item>
        <el-form-item label="点位名称" prop="description">
          <el-input v-model="binForm.description" placeholder="点位名称" />
        </el-form-item>
        <el-form-item label="数据类型" prop="dataType">
          <el-select
            v-model="binForm.dataType"
            placeholder="数据类型"
            clearable
            filterable
            style="width: 240px"
          >
            <el-option
              v-for="dict in data_type"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input
            v-model="binForm.remark"
            type="textarea"
            :maxlength="200"
            show-word-limit
            placeholder="请输入内容"
          ></el-input>
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Data">
import useDictStore from "@/store/modules/dict";
import { useDict } from "@/utils/dict";
import {
  optionselect as getDictOptionselect,
  getType,
} from "@/api/system/dict/type";
import {
  listEquipment,
  getEquipment,
  delEquipment,
  addEquipment,
  updateEquipment,
} from "@/api/base/equipment/equipmentApi";
import {
  addProdEquipmentPointPositionInfo,
  updateProdEquipmentPointPositionInfo,
  queryProdEquipmentPointPositionInfoList,
  deleteProdEquipmentPointPositionInfo,
  getProdEquipmentPointPositionData,
} from "@/api/base/equipment/ProdEquipmentPointPositionApi";
import { nextTick, reactive, useAttrs } from "vue";
const { proxy } = getCurrentInstance();

const { data_type, equipment_status } = proxy.useDict(
  "data_type",
  "equipment_status"
);
const attrs = useAttrs();
const emit = defineEmits(["close", "setTitle"]);
const loading = ref(true);
const showSearch = ref(true);
const open = ref(false);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");
const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    equipNo: undefined,
    name: undefined,
    status: undefined,
    equipId: undefined,
  },
  rules: {
    identification: [
      { required: true, message: "点位编号不能为空!", trigger: "blur" },
    ],
    description: [
      { required: true, message: "点位名称不能为空!", trigger: "blur" },
    ],
    equipNo: [{ required: true, message: "请输入设备编号!", trigger: "blur" }],
  },
  binForm: {},
});
const pointData = ref([]);
const activeName = ref("first");

const formRef = ref("formRef");
const { form, queryParams, rules, binForm } = toRefs(data);

function getDetail(id) {
  reset();
  getEquipment(id).then((res) => {
    form.value = res.data;
    getList();
  });
}

/** 查询字典类型列表 */
function getList() {
  loading.value = true;
  queryParams.value.equipNo = form.value.equipNo;
  queryParams.value.equipId = form.value.id;
  queryProdEquipmentPointPositionInfoList(queryParams.value).then(
    (response) => {
      pointData.value = response.rows;
      total.value = response.total;
      loading.value = false;
    }
  );
}

/** 取消按钮 */
function cancel() {
  open.value = false;
  reset();
}

function handleClose() {
  emit("close");
}

/** 表单重置 */
function reset() {
  binForm.value = {
    identification: undefined,
    description: undefined,
    equipCode: form.value.equipNo,
    equipId: form.value.id,
    equipName: form.value.name,
    equipNo: form.value.equipNo,
    remark: undefined,
    dataType: undefined,
  };
  // formRef.value.resetFields();
  proxy.resetForm("formRef");
}
/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}
/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加点位";
}

/** 多选框选中数据 */
function handleSelectionChange(selection) {
  ids.value = selection.map((item) => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const equipId = row.id || ids.value;
  getProdEquipmentPointPositionData(equipId).then((response) => {
    binForm.value = response.data;
    open.value = true;
    title.value = "修改点位信息";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["formRef"].validate((valid) => {
    if (valid) {
      if (binForm.value.id != undefined) {
        updateProdEquipmentPointPositionInfo(binForm.value).then((response) => {
          proxy.$modal.msgSuccess("编辑成功");
          open.value = false;
          getList();
        });
      } else {
        addProdEquipmentPointPositionInfo(binForm.value).then((response) => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const idList = row.id || ids.value.join(",");
  let msg = '是否确认删除所选的数据项？'
  if(row.description){
    msg = `是否确认删除点位名称为“${row.description}”的数据项？`
  }
  proxy.$modal
    .confirm(msg)
    .then(function () {
      return deleteProdEquipmentPointPositionInfo({ ids: idList });
    })
    .then(() => {
      getList();
      proxy.$modal.msgSuccess("删除成功");
    })
    .catch(() => {});
}

onMounted(async () => {
  const { data } = attrs;
  getDetail(data.id);
});
</script>
